public with sharing class AddCallRecordControl
{
        //get url id
    public String UrlId
    {
        get{        
                return System.currentPageReference().getParameters().get('id');     
           }  
        set;
    }

    public String msg{get;set;}
    
    public List<CallPlan__c> CallPlanList{
        get{
            
            List<MonthlyCallPlan__c> mcpl = [Select Id, Name, PlanMonth__c, PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
            if(mcpl.size()>0){
                String month_s  =  mcpl.get(0).PlanMonth__c;
                String year_s  =  mcpl.get(0).PlanYear__c;
                month_s = month_s.replace('月','');
                Integer month_i = Integer.valueof(month_s);
                Integer year_i = Integer.valueof(year_s);
                if((date.today().year() != year_i)||(date.today().month() != month_i))
                {
                    msg = '您所进入的月度拜访计划不是当月计划';
                    return null;
                }               
            }           
            
            if(CallPlanList == null){
                CallPlanList = new List<CallPlan__c>();
                                               
                List<CallPlan__c> CallPlanAll = [Select comments__c,PlanType__c,Name,CallPlanTime__c,CallDate__c,JointCallExecution__c,
                    CallWorkingtype__c,Id,Achievecallpurpose__c,CallExecutionstatus__c,HCP__c,Hospital__c,HCPDepartment__c
                     From CallPlan__c Where Monthly_Call_Plan__r.OwnerId  =: UserInfo.getUserId()];
                for(CallPlan__c add:CallPlanAll)
                {
                    if(add.CallDate__c.date() == Date.today())
                    {
                        CallPlanList.add(add);
                    }
                }
            }                       
            return CallPlanList;  
        }
        set;
    }

    public CallPlan__c cpDate{
        get{
            if(cpDate == null){
                cpDate = new CallPlan__c();             
            }
            cpDate.CallPlanDate__c = Date.today();
            return cpDate;
        }
        set;    
    }   

    public PageReference Save()
    {
        try{
        if(CallPlanList== null)
            return null;
        for(CallPlan__c cp:CallPlanList)
        {
            if((cp.CallExecutionstatus__c == null)&&(cp.CallWorkingtype__c == '拜访')
                &&(cp.Achievecallpurpose__c != null))
            {
                msg = '请确认拜访执行情况';
                return null;
            }
            
            if(cp.CallPlanTime__c == 'AM(9am-12am)')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    cp.CallDate__c =  datetime.newInstance( cpDate.CallPlanDate__c, starttime );  
                }
            }
            if(cp.CallPlanTime__c == 'PM(2pm-5pm)')
            {
                Time starttime = Time.newInstance(14, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    cp.CallDate__c =  datetime.newInstance( cpDate.CallPlanDate__c, starttime );               
                }
            }   
        }    
        update CallPlanList;     
 
        //go back to region list page
        pageReference pr = new pageReference('/'+ UrlId);     
        pr.setredirect(true);      
        return pr;
        }
        catch (DmlException e) {
            ApexPages.addMessages(e);
            return null;
         }
        
    }
    public PageReference AddOutPlan()
    {
    
            List<MonthlyCallPlan__c> mcpl = [Select Id, Name, PlanMonth__c, PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
            if(mcpl.size()>0){
                String month_s  =  mcpl.get(0).PlanMonth__c;
                String year_s  =  mcpl.get(0).PlanYear__c;
                month_s = month_s.replace('月','');
                Integer month_i = Integer.valueof(month_s);
                Integer year_i = Integer.valueof(year_s);
                /*
                if((date.today().year() != year_i)||(date.today().month() != month_i))
                {
                    msg = '您所进入的月度拜访计划不是当月计划';
                    return null;
                }
                */               
            }     
       
        pageReference pr = page.AddOutPlanRecord;
        pr.getParameters().put('id', UrlId);
        return pr;
    }
    
        public PageReference Cancel()
    {   
        pageReference pr = new pageReference('/'+ UrlId);     
        pr.setredirect(true);      
        return pr;
    }
    
    static testMethod void AddCallRecordControlTest()
    {
        Profile p  = [select id from profile where name = 'System Administrator' or name =: '系统管理员'];
        
        User u1 = new User (alias = 'standt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='mjnproject@isoftstonetestclassaccount.com');
        insert u1;

    System.runAs(u1) {
        Integer month_i = date.today().month();
        Integer year_i = date.today().year();
        string month_s = string.valueof(month_i) + '月';
        string year_s = string.valueof(year_i);
                        
        MonthlyCallPlan__c mcp = new MonthlyCallPlan__c();
        mcp.OwnerId = u1.Id;   
        mcp.PlanMonth__c = month_s;
        mcp.PlanYear__c = year_s;
        insert mcp;
        
        
        CallPlan__c cp = new CallPlan__c();
        cp.name = 'a';
        cp.Monthly_Call_Plan__c = mcp.Id;
        cp.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）';
        cp.CallPurpose__c = '商谈专业评估';
        cp.CallPlanTime__c = 'AM(9am-12am)';
       
        cp.Lasts__c = 3;
        cp.CallDate__c = dateTime.Now();
        cp.CallPlanDate__c = date.Today();
        cp.PlanType__c = '计划外';
        
        Hospital__c hos = new Hospital__c();
        hos.Name = 'aaaaaaaa';
        hos.Nickname__c = 'aaaaaaaaaaa';
        insert hos;
        
        HCP__c hcp= new HCP__c();
        hcp.class1__c = '全国学术带头人(NKOL)';
        hcp.Hospital__c = hos.Id;
        insert hcp;   
        
        cp.HCP__c = hcp.Id;  
        insert cp;
        
     
        
        AddCallRecordControl testCase = new AddCallRecordControl();
        
        test.setCurrentPageReference(Page.AddCallRecord);
        System.currentPageReference().getParameters().put('id', mcp.Id);    
               
        System.assert(testCase.CallPlanList != null);
        System.assert(testCase.msg == null);
        System.assert(testCase.cpDate != null);
        System.assert(testCase.UrlId != null);
              
        testCase.AddOutPlan();
        testCase.Save();
        testCase.Cancel();
        }
    }    
}